<?php
//include 'function.php';

class VideoController extends Zend_Controller_Action
{
  private $db;
  private $content_table;
  private $history;
  public function init()
  {
    $db_obj = new myDb();
    $this->db = $db_obj->getDb();

    Zend_Db_Table::setDefaultAdapter($this->db);
    $this->content_table = new Application_Model_DbTable_Content();
    $this->history = new Application_Model_DbTable_History();
  }

  public function indexAction()
  {

  }

  // param like: 
  // array('name'=>$name,
  // 	'path'=>$path,
  // 	'format'=>$format,
  // 	'size'=>$size,
  // 	'uid'=>$uid,
  // 	'thumb'=>$thumb)
  static public function insertAction($param)
  {
    $this->db->insert($param);
  }
  public function playAction()
  {
    $cont_id = $this->getRequest()->getParam('cont_id');

    $where = $this->db->quoteInto('id = ?', $cont_id);
    //$this->view->cont = $this->content_table->fetchRow($where);    

    $select = $this->db->select();
    $select->from(array("c"=>"content"), array("cont_id"=>"c.id", "c.path", "c.title", "c.thumb", "c.ori_price", "c.dis_price", "c.promotion", "c.descript", "c.say_good", "c.say_bad", "cat_id"=>"cat.id", "cat_name"=>"cat.name", "t.tag"))
      ->joinLeft(array("cat"=>"category"), "cat.id = c.cat_id")
      ->joinLeft(array("t"=>"tags"), "c.id = t.cont_id")
      ->where("c.id={$cont_id}");
    $course = $this->db->fetchRow($select);
    $this->view->course = $course;
    
    /* check whether need to pay */
    $current_price = $course['promotion'] ? $course['dis_price'] : $course['ori_price'];
    if ($current_price > 0)
      {
	/* check whether user login */
	Zend_Session::start();
	$session = new Zend_Session_Namespace('account');   
	if (!isset($session->user) || $session->user->get("id") < 1)
	  {
	    header("Location: /account/login");
	    die();
	  }

	/* check whether user broght this course */
	$pay_table = new Application_Model_DbTable_PayHistory();
	$where = $this->db->quoteInto('buyer_id = ?', $session->user->get("id")) . ' and '.
	  $this->db->quoteInto('cont_id = ?', $cont_id);
	if ((count($pay_table->fetchRow($where)) < 1) && ($session->user->get("id") != $course['uid']))
	  {
	    header("Location: /payment/buy-content-by-id?cont_id=$cont_id");
	    die();
	  }
      }

    // insert into view history
    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');   
    if ($session->user->get("id"))
      $this->history->insert(array('uid'=>$session->user->get("id"), 'cont_id'=>$cont_id));

    // get comments
    /* get the comments list */
    $select = $this->db->select();
    $select->from(array("c"=>"comment"), array("u.username", "u.email", "c.comment", "c.time"))
      ->join(array("u"=>"users"), "c.uid = u.id")
      ->where("c.cont_id={$cont_id}")
      ->order("time desc");
    $comments = $this->db->fetchAll($select);

    // set pageinator
    $page = 1;
    /* $numPerPage = 3; */
    if(isset($_GET['page']) && is_numeric($_GET['page'])){
      $page = $_GET['page'];
    }
    $paginator = Zend_Paginator::factory($comments);
    $paginator->setCurrentPageNumber($page)->setItemCountPerPage(COMMENT_PER_PAGE); 
    $this->view->paginator = $paginator;    

    // get author's information
    $select = $this->db->select();
    $select->from(array("u"=>"users"), array("u.email","u.realname","u.signature","college_name"=>"co.name", "school_name"=>"s.name"))
      ->join(array("c"=>"content"), "u.id = c.uid")
      ->join(array("s"=>"school"), "u.school_id = s.scid")
      ->join(array("co"=>"college"), "u.college_id = co.coid")
      ->where("c.id={$cont_id}");
    $this->view->author = $this->db->fetchRow($select);
  }

  public function commentAction()
  {
    $comment = $this->getRequest()->getParam('comment');
    $cont_id = $this->getRequest()->getParam('cont_id');

    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');   
    // must login before continue
    if (!$session->user->get("id"))
      {
	print urldecode(json_encode(array('resp_id'=>1, 'resp_str'=>urlencode("请登录后再评论"))));
	die();
      }

    // every user can comment only one time
    $table = new Application_Model_DbTable_Comment();    
    $where = $this->db->quoteInto('uid = ?', $session->user->get("id")).
      $this->db->quoteInto(' and cont_id = ?', $cont_id);
    $say = $table->fetchAll($where);
    if (count($say)>0)
      {
	print urldecode(json_encode(array('resp_id'=>2, 'resp_str'=>urlencode("你已经评论过了"))));
	die();	
      }
    
    // insert the comment
    $res = $table->insert(array('uid'=>$session->user->get("id"), 'cont_id'=>$cont_id, 'comment'=>$comment));
    $this->view->result = json_encode(array("resp_id"=>0, "resp_str"=>$res, 'username'=>get_true_value($session->user->get("username"), $session->user->get("email")), 'comment'=>$comment, 'time'=>date("Y-m-d H:i:s")));
  }

  public function sayGoodAction()
  {
    $cont_id = $this->getRequest()->getParam('cont_id');

    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');   
    // must login before continue
    if (!$session->user->get("id"))
      {
	print urldecode(json_encode(array('resp_id'=>1, 'resp_str'=>urlencode("请登录后再来顶"))));
	die();
      }

    // every user can say good or bad once to one content
    $say_good_table = new Application_Model_DbTable_SayGoodOrBad();
    $where = $this->db->quoteInto('uid = ?', $session->user->get("id")).
      $this->db->quoteInto(' and cont_id = ?', $cont_id);
    $say = $say_good_table->fetchAll($where);
    if (count($say)>0)
      {
	print urldecode(json_encode(array('resp_id'=>2, 'resp_str'=>urlencode("你已经顶/踩过了"))));
	die();	
      }

    // insert into Application_Model_DbTable_SayGoodOrBad table
    $say_good_table->insert(array('uid'=>$session->user->get("id"), 'cont_id'=>$cont_id, 'say'=>1));

    // update say_good in content
    $where = $this->db->quoteInto('id = ?', $cont_id);
    $table = new Application_Model_DbTable_Content();    
    $cont = $table->fetchRow($where);
    $new_good = $cont['say_good']+1;
    $table->update(array('say_good'=>$new_good), $where);
    $this->view->result = $table->fetchRow($where);
  }
  public function sayBadAction()
  {
    $cont_id = $this->getRequest()->getParam('cont_id');

    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');   
    // must login before continue
    if (!$session->user->get("id"))
      {
	print urldecode(json_encode(array('resp_id'=>1, 'resp_str'=>urlencode("请登录后再来踩"))));
	die();
      }

    // every user can say good or bad once to one content
    $say_good_table = new Application_Model_DbTable_SayGoodOrBad();
    $where = $this->db->quoteInto('uid = ?', $session->user->get("id")).
      $this->db->quoteInto(' and cont_id = ?', $cont_id);
    $say = $say_good_table->fetchAll($where);
    if (count($say)>0)
      {
	print urldecode(json_encode(array('resp_id'=>2, 'resp_str'=>urlencode("你已经顶/踩过了"))));
	die();	
      }

    // insert into Application_Model_DbTable_SayGoodOrBad table
    $say_good_table->insert(array('uid'=>$session->user->get("id"), 'cont_id'=>$cont_id, 'say'=>0));

    $where = $this->db->quoteInto('id = ?', $cont_id);
    $table = new Application_Model_DbTable_Content();    
    $cont = $table->fetchRow($where);
    $new_bad = $cont['say_bad']+1;
    $table->update(array('say_bad'=>$new_bad), $where);
    $this->view->result = $table->fetchRow($where);
  }
  public function addBookmarkAction()
  {

  }

}
